package com.taotao.store.order.controller;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.CommonResult;
import com.taotao.common.pojo.EasyUIResult;
import com.taotao.store.order.pojo.Order;
import com.taotao.store.order.service.OrderService;

@RequestMapping("/order")
@Controller
public class OrderController {

	@Autowired
	private OrderService orderService;

	/**
	 * 创建订单
	 * 
	 * @param json
	 * @return
	 */
	@ResponseBody
	@PostMapping(value = "/create")
	public CommonResult createOrder(@Valid @RequestBody Order order, BindingResult result) {

		try {
			if (result.hasErrors()) {
				List<String> msgs = new ArrayList<>();
				for (ObjectError error : result.getAllErrors()) {
					msgs.add(error.getDefaultMessage());
				}
				String message = StringUtils.collectionToCommaDelimitedString(msgs);
				return CommonResult.build(400, message);
			}
			// 创建订单返回订单号
			String number = this.orderService.createOrder(order);
			return CommonResult.ok(number);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return CommonResult.build(500, "服务器内部错误！");
	}

	/**
	 * 根据订单ID查询订单
	 * 
	 * @param orderId
	 * @return
	 */
	@ResponseBody
	@GetMapping(value = "/query/{orderId}")
	public Order queryOrderById(@PathVariable("orderId") String orderId) {
		return this.orderService.queryOrderById(orderId);
	}

	/**
	 * 根据用户名分页查询订单
	 * 
	 * @param buyerNick
	 * @param page
	 * @param count
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/query/{buyerNick}/{page}/{count}")
	public EasyUIResult<Order> queryOrderByUserNameAndPage(@PathVariable("buyerNick") String buyerNick,
			@PathVariable("page") Integer page, @PathVariable("count") Integer count) {
		return this.orderService.queryOrderByUserNameAndPage(buyerNick, page, count);
	}

	/**
	 * 修改订单状态
	 * 
	 * @param json
	 * @return
	 */
	@ResponseBody
	@PostMapping(value = "/status")
	public CommonResult changeOrderStatus(@RequestBody Order order, BindingResult result) {
		try {
			if(result.hasErrors()){
				List<String> msgs = new ArrayList<>();
				for (ObjectError error : result.getAllErrors()) {
					msgs.add(error.getDefaultMessage());
				}
				return CommonResult.build(400, StringUtils.collectionToCommaDelimitedString(msgs));
			}
			Boolean b = this.orderService.changeOrderStatus(order);
			if(b){
				return CommonResult.ok("更新成功");
			}
			return CommonResult.build(404, "要更新的资源不存在");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return CommonResult.build(500, "服务器错误！");
	}
}
